﻿using IR.Common.Enums;
using IR.Controller.ModelControllers;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IR.WPF.UI.ViewModels
{
    public class VMVersionEdit : ViewModelBase
    {
        private CVersion _versionToEdit;
        private string _typeOfPage;
        private bool _isOld;
        private ICommand _cancelCommand;
        private ICommand _saveCommand;

        private string initialPK;

        public VMVersionEdit(CVersion versionToEdit = null)
        {
            if (versionToEdit == null)
            {
                _versionToEdit = new CVersion();
                _isOld = false;
                _typeOfPage = "New Version";
            }
            else
            {
                _versionToEdit = versionToEdit;
                _isOld = true;
                initialPK = _versionToEdit.VersionID;
                _typeOfPage = "Edit Version";
            }
        }

        #region **Properties**
        public string TypeOfPage
        {
            get { return _typeOfPage; }
            set { _typeOfPage = value; }
        }

        public string VersionID
        {
            get { return _versionToEdit.VersionID; }
            set { _versionToEdit.VersionID = value; }
        }

        public Status Status
        {
            get { return _versionToEdit.Status; }
            set { _versionToEdit.Status = value; }
        }

        public IEnumerable<Status> StatusTypeValues
        {
            get
            {
                return Enum.GetValues(typeof(Status))
                    .Cast<Status>();
            }
        }
        #endregion

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Cancel
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new RelayCommand(p => CancelFunction(p as Control), p => CanCancel(p as Control));

                return _cancelCommand;
            }
        }

        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Save
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new RelayCommand(p => SaveFunction(p as Control), p => CanSave(p as Control));

                return _saveCommand;
            }
        }
        #endregion

        #region **Command Functions**
        protected void CancelFunction(Control sender)
        {
            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            window.Close();
        }

        protected void SaveFunction(Control sender)
        {
            bool canSave = true;
            bool saved = false;
            bool inputError = false;

            if (_isOld == false)
            {
                _versionToEdit.DateCreated = System.DateTime.Now;
            }
            else
            {
                if (initialPK != _versionToEdit.VersionID)
                    canSave = false;
            }

            if (_versionToEdit.Status == Status.badvalue
                || string.IsNullOrEmpty(_versionToEdit.VersionID))
            {
                inputError = true;
                MessageBox.Show("Versions must have a name and real status.");
            }

            if (inputError == false)
            {
                if (canSave == false)
                {
                    MessageBox.Show("You can't change the VersionID.");
                }
                else
                {
                    if (_isOld == false)
                    {
                        saved = _versionToEdit.Save();
                    }
                    else
                    {
                        saved = _versionToEdit.Save(true);
                    }

                    // Get a reference to the Window that contained the sender Control
                    if (saved == false)
                    {
                        MessageBox.Show("This was not saved properly.");
                    }
                    else
                    {
                        Window window = Window.GetWindow(sender);

                        window.Close();
                    }
                }
            }
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanCancel(Control sender)
        {
            return true;
        }

        protected bool CanSave(Control sender)
        {
            return true;
        }
        #endregion
    }
}
